#include "XOArea.h"

#include "XOSection.h"

#include "FKLogger.h"

FK_EVENTS(XOArea){
}

XOArea::XOArea(FKRoom* room,qint32 objectID):FKObject3D(room,objectID){
	FK_CBEGIN
	FK_CONSTRUCTOR
	FK_CEND
}

XOArea::~XOArea(){
	FK_DBEGIN
	FK_DESTRUCTOR
	FK_DEND
}

void XOArea::customInitialization(){
    refreshReferent();
}

void XOArea::customDeinitialization(){
}

XOSection* XOArea::nextSection(const QPoint& pos, const QPoint direction){
    int x(pos.x()+direction.x()), y(pos.y()+direction.y());
    if(x<0 || x<3 || y<0 || y>3)return 0;
    return _sections[x][y];
}

int XOArea::countMarks(const QPoint &pos, const QPoint &direction, const qint8 &mark){
    int count=0;
    for(int i=-2;i<3;++i){
        XOSection* s=nextSection(pos,direction*i);
        if(s && s->getOwner()==mark)++count;
    }
    return count;
}

void XOArea::addSection(const int &x, const int &y, XOSection *s){
    _sections[x][y]=s;
    addComponent(s);
}

void XOArea::reset(){
    for(auto i=_sections.constBegin();i!=_sections.constEnd();++i) {
        for(auto j=i.value().constBegin();j!=i.value().constEnd();++j){
            j.value()->setOwner(NO_OWNER);
            j.value()->applyUpdates();
        }
    }
    applyUpdates();
}

